
Chapter 5: Unit Testing 101
In Chapter 1, we took the first steps toward “slow and steady wins the race” testing. Let’s continue
on this path.
My Struggles
One unfortunate truth of learning how to test is that so much of the information on the web and in
books contradicts one another. As a student, I’d read one article, think I had a decent grasp on the
terminology and rules, and then move on to the next article that, in many ways, preached an entirely
different set of guidelines. Imagine learning what two plus two equals, when all the resources provide
different answers?
What’s the reason for this dissonance in the community? Well, it’s really quite simple: we’re still
figuring this stuff out! Even though the concept of testing software goes as far back as the seventies
- during the days of Smalltalk²¹ - the truth is that, even today, the development community remains
divided on many issues, including such basic things as whether testing is beneficial. As a result,
as you continue learning beyond this book, be prepared to discover different methodologies and
terminologies, dependent upon a number of factors, including the article’s publish date, which
language it was written for, and what belief system its writer has.
The differing view points were only the beginning of my struggles, though. As I continued digging
in, I didn’t yet understand or appreciate the various types of testing. As such, I’d learn from one
source that testing in isolation was paramount, and then move on to find countless other articles,
where the authors seemed to ignore this rule, in favor of touching every part of the system. Of course,
now, I realize that these articles focused on different types of testing (acceptance or functional vs.
unit), but, back then, I was dumbfounded and overwhelmed.
I suppose what I want you to understand and appreciate is that we’re still in the early days of test-
driven development, and testing in general. Even worse, while the Ruby on Rails community is a
culture that has widely embraced and evangelized testing, the same unfortunately can’t be said for
the PHP world. But, we’re fixing that, right? The tides are turning; I hope you’re on board!
In this chapter, we’ll focus exclusively on unit testing with test-driven development. If you’ve come
across other types of testing in your web travels - perhaps ones that do hit databases and query
APIs - rest assure that they absolutely have their place. In fact, in real world projects, you’ll have a
number of test suites, each which exercises your application in slightly different ways. That being
said, this chapter is not concerned with those other styles of testing; we’ll get to them in due time.
For now, though, and as a first step into the testing waters, let’s focus exclusively on one object at a
time.
²¹http://en.wikipedia.org/wiki/Smalltalk
50